public class code2 {
    //最长回文子串：在回文子串的基础上再找最长
    public String longestPalindrome(String s) {
        //先判断有多少个回文子串
        boolean[][] dp = new boolean[s.length()][s.length()];
        int len = 0;
        int beg = 0;
        for(int i=s.length()-1;i>=0;i--) {
            for(int j=i;j<s.length();j++) {
                if(s.charAt(i)==s.charAt(j)) {
                    dp[i][j] = (i+1>=j?true:dp[i+1][j-1]);
                }
                //找出最长子串
                if(dp[i][j] && j-i+1>len) {
                    len = j-i+1;
                    beg = i;
                }
            }
        }
        return s.substring(beg,len+beg);
    }
}
